package net.xuele.learn.flux;

import java.util.concurrent.TimeUnit;

/**
 * 由于加载过程串行执行的关系，导致消耗实现线性累加。Blocking 模式即串行执行
 */
public class DataLoader {


    public final void load() {
        long startTime = System.currentTimeMillis();
        doLoad();
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("总耗时为："+ costTime + "毫秒");
    }

    protected void doLoad(){
        loadConfigurations();
        loadUsers();
        loadOrders();
    }

    /**
     * 加载订单3s
     */
    protected void loadOrders() {
        loadMock("loadOrders()", 3);
    }

    /**
     * 加载用户2s
     */
    protected void loadUsers() {
        loadMock("loadUsers()", 2);
    }

    /**
     * 加载配置1s
     */
    protected final void loadConfigurations() {
        loadMock("loadConfigurations()", 1);
    }

    private void loadMock(String source, int seconds) {
        try {
            long startTime = System.currentTimeMillis();
            long milliseconds = TimeUnit.SECONDS.toMillis(seconds);
            Thread.sleep(milliseconds);

            long costTime = System.currentTimeMillis() - startTime;

            System.out.printf("[线程 : %s] %s 耗时 :  %d 毫秒\n", Thread.currentThread().getName(),
                                    source, costTime);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        new DataLoader().load();
    }
}
